package com.superjson.showpro.shared.util;

import com.beantools.constants.SingleChar;
import com.beantools.util.ObjectUtil;
import com.beantools.util.SignUtill;
import com.superjson.showpro.shared.exception.GeneralBizException;
import com.superjson.showpro.shared.result.BaseResultCodeEnum;

import java.io.File;
import java.util.UUID;

import static com.beantools.constants.SingleChar.FORWARD_LEAN;
import static com.beantools.constants.SingleChar.INVERSE_LEAN;
import static com.beantools.util.StringUtil.isBlank;

/**
 * Created By zby on 13:26 2019/6/10
 */

public class CommonUtil {

    /**
     * Created By zby on 13:27 2019/6/10
     */
    public static String getRandomAccountName() {
        //最大支持1-9个集群机器部署
        int machineId = 1;
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if (hashCodeV < 0) {
            hashCodeV = Math.abs(hashCodeV);
        }
        return machineId + String.format("%011d", hashCodeV);
    }

    public static void ifNullThrow(Object obj, String msg) {
        if (ObjectUtil.isNull(obj)) {
            throw new GeneralBizException(msg);
        }
    }

    public static void ifNullThrow(Object obj, BaseResultCodeEnum baseResultCodeEnum) {
        if (ObjectUtil.isNull(obj)) {
            throw new GeneralBizException(baseResultCodeEnum);
        }
    }

    public static void ifTrueThrow(boolean isTrue, String msg) {
        if (isTrue) {
            throw new GeneralBizException(msg);
        }
    }

    public static void ifTrueThrow(boolean isTrue, BaseResultCodeEnum baseResultCodeEnum) {
        if (isTrue) {
            throw new GeneralBizException(baseResultCodeEnum);
        }
    }

    /**
     * Created By zby on 17:00 2019/6/15
     * 获取quill真正的字符串
     */
    public static String getQuillContent(String content) {
        if (isBlank(content)) {
            throw new NullPointerException("文本内容不能为空");
        }
        String header = "<div class=\"ql-editor\" data-gramm=\"false\" contenteditable=\"true\" data-placeholder=\"请评论这个类或方法\">";
        String end = "</div><div class=\"ql-clipboard\" contenteditable=\"true\" tabindex=\"-1\"></div><div class=\"ql-tooltip ql-hidden\"><a class=\"ql-preview\" target=\"_blank\" href=\"about:blank\"></a><input type=\"text\" data-formula=\"e=mc^2\" data-link=\"https://quilljs.com\" data-video=\"Embed URL\"><a class=\"ql-action\"></a><a class=\"ql-remove\"></a></div>";
        int haderLen = header.length();
        int endLen = end.length();
        content = content.substring(haderLen);
        content = content.substring(0, content.length() - endLen);
        return content;
    }

    /**
     * Created By zby on 15:24 2019/3/21
     * 通过spring自带的方法找到字节码classes包的路径，
     * 再往上找三级目录，到Tomcat的webApps下目录，
     * 当前目录加上 /upload才是图片路径
     */
    public static String getTomcatRootPath() {
        String absolutePath = new File("").getAbsolutePath();
        absolutePath = absolutePath.substring(0, absolutePath.lastIndexOf(absolutePath.contains(SingleChar.INVERSE_LEAN) ? SingleChar.INVERSE_LEAN : SingleChar.FORWARD_LEAN));
        return absolutePath;
    }

}
